/*
** Copyright (C) 2009 Joshua Brent Marsh <joshua@icub3d.com>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**  
*/

/* $Id: buffer.h 9 2009-10-08 15:50:15Z joshua@icub3d.com $ */

#ifndef ICUB3D_BUFFER_H
#define ICUB3D_BUFFER_H 1

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

typedef
struct buffer
{
  size_t size;      /* The size of the buffer. */
  size_t position;  /* The buffer's current position. */
  size_t limit;     /* The current limit of the buffer. */
  size_t mark;      /* The current mark of the buffer. */
  void *data;       /* The memory of the buffer. */
} Buffer;

Buffer * buffer_create (size_t);
void buffer_destroy (Buffer *, int);

size_t buffer_size (Buffer *);
void buffer_compact (Buffer *);

void buffer_clear (Buffer *);
void buffer_flip (Buffer *);
void buffer_mark (Buffer *);
void buffer_reset (Buffer *);
void buffer_rewind (Buffer *);

size_t buffer_remaining (Buffer *);

size_t buffer_get_limit (Buffer *);
void buffer_set_limit (Buffer *, size_t);

size_t buffer_get_position (Buffer *);
void buffer_set_position (Buffer *, size_t);

size_t buffer_get (Buffer *, void *, size_t);
size_t buffer_get_abs (Buffer *, void *, size_t, size_t);
size_t buffer_put (Buffer *, void *, size_t);
size_t buffer_put_abs (Buffer *, void *, size_t, size_t);

Buffer * buffer_slice (Buffer *);
Buffer * buffer_wrap (void *, size_t);
Buffer * buffer_dup (Buffer *);

void * buffer_array (Buffer *);
Buffer * buffer_copy (Buffer *);

ssize_t buffer_read (Buffer *, ssize_t *, int);
ssize_t buffer_write (Buffer *, ssize_t *, int);

ssize_t buffer_fread (Buffer *, ssize_t *, FILE *);
ssize_t buffer_fwrite (Buffer *, ssize_t *, FILE *);


#endif /* ICUB3D_BUFFER_H */
